package 字节缓冲流;

import java.io.*;

/*
* 字节缓冲流复制视频
* 创建字节输入流对象：BufferedInputString() 读取数据
* 创建字节输出流对象：BufferedOutputString() 写数据
* 读写数据，复制视频
* 释放资源
* */
public class 字节缓冲流复制视频 {
    public static void main(String[] args) throws IOException {
        /*记录开始时间*/
        long startTime = System.currentTimeMillis();
        /*method1();*/
        /*method2();*/
        /*method3();*/
        method4();
        /*记录结束时间*/
        long endTime = System.currentTimeMillis();
        System.out.println("共耗时:"+(endTime-startTime)+"毫秒");
    }

    /*字符缓冲流一次读写一个字节数组*/
    /*差不多耗费0.5秒*/
    public static void method4() throws IOException {
        /*读取视频*/
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\111\\111.mp4"));
        /*写视频*/
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\111\\222.mp4"));
        byte[] bys = new byte[1024];
        int len;
        while ((len=bis.read(bys))!=-1){
            bos.write(bys,0,len);
        }
        bis.close();
        bos.close();
    }

    /*字符缓冲流一次读写一个字节*/
    /*差不多耗时10几秒*/
    public static void method3() throws IOException {
        /*读取视频*/
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\111\\111.mp4"));
        /*写视频，复制视频*/
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\111\\222.mp4"));
        int by;
        while ((by=bis.read())!=-1){
            bos.write(by);
        }
        bis.close();
        bos.close();
    }

    /*基本字节流一次读写一个字节数组*/
    /*耗时几秒*/
    public static void method2() throws IOException {
        /*读取视频*/
        FileInputStream fis = new FileInputStream("D:\\111\\111.mp4");
        /*复制视频，写视频*/
        FileOutputStream fos = new FileOutputStream("D:\\111\\222.mp4");
        byte[] bys = new byte[1024];
        int len;
        while ((len=fis.read(bys))!=-1){
            fos.write(bys,0,len);
        }
        fos.close();
        fis.close();
    }

    /*这种方法一个字节一个字节的读取，需要花费很多时间*/
    /*基本字节流一次读写一个字节*/
    public static void method1() throws IOException {
        /*读取视频*/
        FileInputStream fis = new FileInputStream("D:\\111\\111.mp4");
        /*写视频*/
        FileOutputStream fos = new FileOutputStream("D:\\111\\222.map4");
        int by;
        while ((by=fis.read())!=-1){
            fos.write(by);
        }
        fos.close();
        fis.close();
    }
}